import LogParent from './logParent'

export default class Log extends LogParent {
    constructor(db) {
        super(db, 'logs')
    }

    /**
     * 统计planId的所有成功/失败的步骤数目
     * @param {string} planId 
     */
    async statLogsOfPlan(planId) {
        const passCountMap = await this.groupBy({ planId, type: 'step', status: 'pass' }, '$suiteId')
        const failureCountMap = await this.groupBy({ planId, type: 'step', status: 'failure' }, '$suiteId')
        const finalMap = {}

        passCountMap
            .forEach(({ id: suiteId, count }) => {
                !finalMap[suiteId] && (finalMap[suiteId] = { pass: 0, failure: 0 })
                finalMap[suiteId].pass = count
            })

        failureCountMap
            .forEach(({ id: suiteId, count }) => {
                !finalMap[suiteId] && (finalMap[suiteId] = { pass: 0, failure: 0 })
                finalMap[suiteId].failure = count
            })

        return finalMap
    }

    async updateRemark(id, remark) {
        const result = await this.updateById(id, { remark })
        return result
    }
}